package com.iqiyi.pps.epg.core.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.message.Processor;
import com.iqiyi.pps.epg.core.model.channel.ChannelBase;
import com.iqiyi.pps.epg.core.model.channel.ChannelBoss;
import com.iqiyi.pps.epg.core.model.program.ProgramData;
import com.iqiyi.pps.epg.core.model.program.ProgramLog;
import com.iqiyi.pps.epg.core.model.program.TicketChangeAssist;
import com.iqiyi.pps.epg.core.model.program.VVChangeAssist;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.program.IProgramAsyncService;
import com.iqiyi.pps.epg.core.service.program.ProgramService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.WriteSystemLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;

/**
 * Created by lemuel on 2016/11/17.
 */
public class BossMessageProcessor extends Processor {
    private final Logger logger = LoggerFactory.getLogger(BossMessageProcessor.class);

    public static final int TYPE_PPC = 1;

    private ProgramService programService = (ProgramService) ApplicationContextUtil.getBean(ProgramService.class);

    private ChannelService channelService = (ChannelService) ApplicationContextUtil.getBean(ChannelService.class);

    private IProgramAsyncService getProgramAsyncService() {
        return WriteSystemLogUtil.getInstance().getProgramAsyncService();
    }

    @Override
    public boolean process() {
//        logger.info("[BossMessageProcessor][process] message=" + message.toString());

        try {
            JSONObject bossInfo = JSON.parseObject(message.toString());
            if (null != bossInfo && bossInfo.getIntValue("type") == TYPE_PPC) {
                //logger.info("[BossMessageProcessor][process][message={}]", message);
                long qipuId = bossInfo.getLongValue("entityId");
                ProgramData programData = programService.findProgramDataByQipuId(qipuId);
                if (null != programData) {
                    writeBossMessageLog(programData, message.toString());
                    programData.setBossInfo(message.toString());
                    fillProgramChargeData(programData, bossInfo);
                    programService.editProgramData(programData);

                    doChargeChannel(programData);

                } else {
                    logger.info("[BossMessageProcessor][process][program not exists][qipuId={}]", qipuId);
                }
            }
        } catch (Exception e) {
            logger.info("[BossMessageProcessor][process][message={}] exception = " + e, message.toString());
        }

        finished(true);
        return true;
    }

    private void fillProgramChargeData(ProgramData programData, JSONObject bossInfo) {
        logger.info("[BossMessageProcessor][fillProgramChargeData][qipuId={}]", programData.getQipuId());
        int payType = bossInfo.getIntValue("payType");
        int purchaseType = bossInfo.getIntValue("purchaseType");

        VVChangeAssist changeAssist = new VVChangeAssist(programData,null);
        TicketChangeAssist ticketChangeAssist = new TicketChangeAssist(programData);
        if (payType == 0 && (purchaseType == 1 || purchaseType == 2)) {
            programData.setBossStatus(ProgramData.PAIED_AVAILABLE);
            programData.setChargeStatus(ProgramData.CHARGE_STATUS_PAIED_AVAILABLE);
            programData.setMemberOnly(1);
            if (purchaseType == 1) {
                programData.setMemberFree(1);
            } else {
                programData.setMemberFree(0);
            }
        } else if (payType == 1 && purchaseType == 1) {
            programData.setBossStatus(ProgramData.FREE);
            programData.setChargeStatus(ProgramData.CHARGE_STATUS_PAID_PART);
            programData.setMemberOnly(0);
            programData.setMemberFree(1);
        } else {
//        } else if (purchaseType == 0) {
            programData.setBossStatus(ProgramData.FREE);
            programData.setChargeStatus(ProgramData.CHARGE_STATUS_FREE);
            programData.setMemberOnly(0);
            programData.setMemberFree(1);
        }
        changeAssist.checkAgain(programData,null );
        ChannelBase channelBase = channelService.getChannelBase(programData.getFatherId());
        if(changeAssist.isChange()){
            if(ChannelBase.LIVE_TYPE_LS == channelBase.getLiveType().intValue()){
                getProgramAsyncService().addVVNotify(programData);
            }
        }
//        if (ticketChangeAssist.isChange()) {
//            if(ChannelBase.LIVE_TYPE_LS == channelBase.getLiveType().intValue()){
//               getProgramAsyncService().addTicketSysNotify(programData,null);
//            }
//        }
    }

    private void doChargeChannel(ProgramData programData) {
        logger.info("[BossMessageProcessor][doChargeChannel][start]", programData.getFatherId());
        try {
            ChannelBoss channelBoss = channelService.getChannelBoss(programData.getFatherId());
            if(ProgramData.PAIED_AVAILABLE.equals(programData.getBossStatus()) ){
                channelBoss.setStatus(programData.getBossStatus());
                channelBoss.setIsMemberOnly(programData.getMemberOnly());
                channelBoss.setIsMemeberFree(programData.getMemberFree());
                if (programData.getPaidEndTime() != null) {
                    channelBoss.setPaidEndTime(new Timestamp(programData.getPaidEndTime().getTime()));
                }
                if (programData.getPaidStartTime() != null) {
                    channelBoss.setPaidStartTime(new Timestamp(programData.getPaidStartTime().getTime()));
                }
                channelService.saveBoss(channelBoss);
                channelService.updateChannelSync(programData.getFatherId(), 1);
            }
        } catch (Exception e) {
            logger.error("[BossMessageProcessor][doChargeChannel][error]", e);
        }
    }

    private void writeBossMessageLog(ProgramData programData, String descr) {
        try {
            ProgramLog programLog = new ProgramLog();
            programLog.setProgramId(programData.getId());
            programLog.setUser(Constants.SYSTEM_USER_SIGN);
            programLog.setDescription(descr);
            programLog.setType(Constants.LogType.BOSS_PAID_MESSAGE.getIndex());
            programService.saveProgramLog(programLog);
        } catch (Exception e) {
            logger.error("[BossMessageProcessor][writeBossMessageLog][error]", e);
        }
    }
}
